package grape.house.rest.estatephase.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.common.rest.mvc.BaseController;
import grape.house.rest.estatephase.form.EstatePhaseCreateForm;
import grape.house.rest.estatephase.form.EstatePhaseListForm;
import grape.house.rest.estatephase.form.EstatePhaseListPageForm;
import grape.house.rest.estatephase.form.EstatePhaseUpdateForm;
import grape.house.rest.estatephase.mapper.EstatePhaseWebMapper;
import grape.house.rest.estatephase.vo.EstatePhaseVo;
import grape.house.service.estatephase.api.IEstatePhaseService;
import grape.house.service.estatephase.po.EstatePhase;
import grape.house.service.estatephase.query.EstatePhaseListQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
/**
 * <p>
 * 楼盘分期 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/house/estatephase")
@Api(tags = "楼盘分期相关接口")
public class EstatePhaseController extends BaseController<EstatePhaseVo, EstatePhase> {

    @Autowired
    private EstatePhaseWebMapper currentWebMapper;
    @Autowired
    private IEstatePhaseService currentService;



     @ApiOperation("添加楼盘分期")
     @PreAuthorize("hasAuthority('estatephase:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public EstatePhaseVo create(@RequestBody @Valid EstatePhaseCreateForm cf) {

         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }

         EstatePhase po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询楼盘分期")
     @PreAuthorize("hasAuthority('estatephase:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public EstatePhaseVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除楼盘分期")
     @PreAuthorize("hasAuthority('estatephase:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新楼盘分期")
     @PreAuthorize("hasAuthority('estatephase:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public EstatePhaseVo update(@PathVariable String id,@RequestBody @Valid EstatePhaseUpdateForm uf) {
         EstatePhase po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }


    @ApiOperation("不分页查询楼盘分期")
    @PreAuthorize("hasAuthority('estatephase:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<EstatePhaseVo> list(EstatePhaseListForm listForm) {
        EstatePhase po = currentWebMapper.formToPo(listForm);
        EstatePhaseListQuery estatePhaseListQuery = new EstatePhaseListQuery();
        estatePhaseListQuery.setPoQuery(po);
        return super.list(estatePhaseListQuery);
    }

    @ApiOperation("分页查询楼盘分期")
    @PreAuthorize("hasAuthority('estatephase:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<EstatePhaseVo> listPage(EstatePhaseListPageForm listPageForm) {
         EstatePhase po = currentWebMapper.formToPo(listPageForm);
        EstatePhaseListQuery estatePhaseListQuery = new EstatePhaseListQuery();
        estatePhaseListQuery.setPoQuery(po);
         return super.listPage(estatePhaseListQuery,listPageForm);
     }

}
